package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;


import cn.wh.xwm.common.util.Constant;
import cn.wh.xwm.controller.action.vo.SelectView;
import cn.wh.xwm.hibernate.dao.Customerinfo;
import cn.wh.xwm.hibernate.dao.CustomerinfoDAO;
import cn.wh.xwm.hibernate.dao.Productinfo;
import cn.wh.xwm.hibernate.dao.ProductinfoDAO;
import cn.wh.xwm.hibernate.dao.UserCustomerRelation;
import cn.wh.xwm.hibernate.dao.UserCustomerRelationDAO;
import cn.wh.xwm.hibernate.dao.Userinfo;
import cn.wh.xwm.hibernate.dao.UserinfoDAO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	
	private String customerlevel;
	private String salesId;
	
	public String initmain(){
		return "initmain";
	}
	
	public String init(){
		return LOGIN;
	}
	/**
	 * 响应json 对象数据
	 * @param object
	 * @throws IOException
	 */
	public void responseJson(JSONObject object) throws IOException {
		HttpServletResponse response=ServletActionContext.getResponse();  
	    response.setContentType("text/html;charset=utf-8");  
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
	    PrintWriter out = response.getWriter();  
	    out.write(object.toString());  
	    System.out.println(object.toString());
	    out.flush();  
	    out.close(); 
	}
	/**
	 * 响应json 数组数据
	 * @param object
	 * @throws IOException
	 */
	public void responseJson(JSONArray array) throws IOException {
		HttpServletResponse response=ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=utf-8");  
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter out = response.getWriter();  
		out.write(array.toString());  
		System.out.println(array.toString());
		out.flush();  
		out.close(); 
	}
	/**
	 * 获取客户级别下拉框；
	 * @throws IOException 
	 */
	public void selectGetCustomerLevel() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = new SelectView();
		sv.setId("1");
		sv.setName(Constant.CUSTOMERLEVEL_1);
		array.add(sv);
		sv = new SelectView();
		sv.setId("2");
		sv.setName(Constant.CUSTOMERLEVEL_2);
		array.add(sv);
		sv = new SelectView();
		sv.setId("3");
		sv.setName(Constant.CUSTOMERLEVEL_3);
		array.add(sv);
		sv = new SelectView();
		sv.setId("4");
		sv.setName(Constant.CUSTOMERLEVEL_4);
		array.add(sv);
		this.responseJson(array);
	}
	
	/**
	 * 通过级别获取客户名称下拉框
	 * @throws IOException 
	 */
	public void selectGetCustomerNameByLevel() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = null;
		CustomerinfoDAO dao = new CustomerinfoDAO();
		String customerlevelString = "";
		switch (Integer.valueOf(customerlevel)) {
		case 1:
			customerlevelString=Constant.CUSTOMERLEVEL_1;
			break;
		case 2:
			customerlevelString=Constant.CUSTOMERLEVEL_2;
			break;
		case 3:
			customerlevelString=Constant.CUSTOMERLEVEL_3;
			break;
		case 4:
			customerlevelString=Constant.CUSTOMERLEVEL_4;
			break;

		}
		List<Customerinfo> list = dao.findByLevel(customerlevelString);
		for(Customerinfo info : list){
			sv = new SelectView();
			sv.setId(info.getId());
			sv.setName(info.getName());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	
	/**
	 * 获取sales辖区内的客户
	 * @throws IOException
	 */
	public void selectGetCustomerNameBySalesId() throws IOException{
		if(salesId==null||salesId.trim().equals("")){
			salesId=this.getUserinfoFromSession().getUserid();
		}
		JSONArray array = new JSONArray();
		SelectView sv = null;
		
		CustomerinfoDAO dao = new CustomerinfoDAO();
		UserCustomerRelationDAO userCustomerRelationDAO =  new UserCustomerRelationDAO();
		List<UserCustomerRelation> list = userCustomerRelationDAO.findByProperty("id.userId", salesId);
		for(UserCustomerRelation info :list){
			Customerinfo cinfo = dao.findById(info.getId().getCustomerId());
			sv = new SelectView();
			sv.setId(cinfo.getId());
			sv.setName(cinfo.getName());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	
	
	
	/**
	 * 获取所有客户名称下拉框
	 * @throws IOException 
	 */
	public void selectGetCustomerName() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = null;
		CustomerinfoDAO dao = new CustomerinfoDAO();
		List<Customerinfo> list = dao.findAll();
		for(Customerinfo info : list){
			sv = new SelectView();
			sv.setId(info.getId());
			sv.setName(info.getName());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	
	/**
	 * 获取所有用户下拉列表
	 * @throws IOException
	 */
	public void selectGetUserName() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = null;
		UserinfoDAO dao = new UserinfoDAO();
		List<Userinfo> list = dao.findAll();
		for(Userinfo info : list){
			sv = new SelectView();
			sv.setId(info.getUserid());
			sv.setName(info.getUsername());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	
	/**
	 * 获取所有salse下拉列表
	 * @throws IOException
	 */
	public void selectGetSalesName() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = null;
		UserinfoDAO dao = new UserinfoDAO();
		List<Userinfo> list = dao.findByRole(Constant.ROLE_SALES);
		for(Userinfo info : list){
			sv = new SelectView();
			sv.setId(info.getUserid());
			sv.setName(info.getUsername());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	
	/**
	 * 获取所有产品下拉列表
	 * @throws IOException
	 */
	public void selectGetProduct() throws IOException{
		JSONArray array = new JSONArray();
		SelectView sv = null;
		ProductinfoDAO dao = new ProductinfoDAO();
		List<Productinfo> list = dao.findAll();
		for(Productinfo info : list){
			sv = new SelectView();
			sv.setId(info.getId());
			sv.setName(info.getName());
			array.add(sv);
		}
		dao.getSession().close();
		this.responseJson(array);
	}
	public Userinfo getUserinfoFromSession(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		Userinfo userinfo = (Userinfo) session.getAttribute(Constant.USER_SESSION);
		return userinfo;
	}
	
	public String getCustomerlevel() {
		return customerlevel;
	}
	public void setCustomerlevel(String customerlevel) {
		this.customerlevel = customerlevel;
	}
	
	public String getServerPath(){
		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac
		.get(ServletActionContext.SERVLET_CONTEXT);
		String path = sc.getRealPath("/");
		return path;
	}
	public String getSalesId() {
		return salesId;
	}
	public void setSalesId(String salesId) {
		this.salesId = salesId;
	}
	
}
